package com.project.fiveCerealsSavoury.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;

import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.fiveCerealsSavoury.dao.ReportFormsDao;
import com.project.fiveCerealsSavoury.domain.ReportForms;
import com.project.fiveCerealsSavoury.domain.Wages;
import com.project.fiveCerealsSavoury.exception.BizException;
import com.project.fiveCerealsSavoury.service.ReportFormsService;
import com.project.fiveCerealsSavoury.transfer.Page;
import com.project.fiveCerealsSavoury.transfer.Report;
import com.project.fiveCerealsSavoury.transfer.UserPage;

@Service
@Transactional
public class ReportFormsServiceImpl implements ReportFormsService {
	@Autowired
	private ReportFormsDao reportFormsDao;

	@Override
	public boolean appendWage(@Valid Wages wages) throws BizException {
		if (wages.getName() == null)
			throw new BizException("名字不为null");
		if (wages.getNum() == null)
			throw new BizException("生产数量不为null");
		if (wages.getMoney() == null)
			throw new BizException("金额不为null");
		reportFormsDao.addWage(wages);
		return true;
	}

	@Override
	public boolean appendRep(Map<Integer, Report> report) throws BizException {
		if (report == null)
			throw new BizException("信息不为null");
		for (int i = 1; i <= 12; i++) {
			ReportForms reportForms = new ReportForms();
			// 获取每月技术工工资
			List<Wages> queryWages = reportFormsDao.queryWages();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  hh-mm-ss");
			Double tmp = 0.0;
			for (Wages wages : queryWages) {
				String format = formatter.format(wages.getTime());
				int indexOf = format.indexOf("-");
				String substring = format.substring(indexOf+1, indexOf+2);//2017-08-12--->0
				String substrings = format.substring(indexOf+1, indexOf+3);//2017-08-12--->08
				if (substring.equals("0")) {//判断是否为0
					String substring1 = format.substring(indexOf+2, indexOf+3);//8和i是否相等
					
					if (substring1.equals(String.valueOf(i))) {
						// 计算技术工每月总金额
						tmp += wages.getMoney() * wages.getNum();
					}
				}else{
					if (substrings.equals(String.valueOf(i))) {
						tmp += wages.getMoney() * wages.getNum();
					}
				}
			}
			Report report2 = report.get(i);
			if (report2 != null) {
				if (String.valueOf(tmp).length() > 4) {
					Double money = tmp/10000.0;
					tmp =Double.valueOf(String.format("%.2f", money));
				}
				reportForms.setMonthSalary(tmp);
				// 搬运工和送货工工资
				Double day = report2.getDayMoneyCarry() * report2.getWeight() + report2.getDayMoneyDelivery() * report2.getWeight();
				if (String.valueOf(day).length() > 4) {
					Double money = day/10000.0;
					day =Double.valueOf(String.format("%.2f", money));
				}
				reportForms.setDaySalary(day);
				Double money =report2.getDayMoneyCarry() * report2.getWeight() + report2.getDayMoneyDelivery() * report2.getWeight()+tmp;
				if (String.valueOf(money).length() > 4) {
					money = money/10000.0;
					money =Double.valueOf(String.format("%.2f", money));
				}
				// 总支出
				reportForms.setPay(money);
				Double balace =report2.getSale() - report2.getDayMoneyCarry() * report2.getWeight() + report2.getDayMoneyDelivery() * report2.getWeight()+tmp;
				if (String.valueOf(balace).length() > 4) {
					balace = balace/10000.0;
					balace =Double.valueOf(String.format("%.2f", balace));
				}
				// 总收入
				reportForms.setIncome(balace);
				reportForms.setTime(report2.getTime());
				reportFormsDao.addRep(reportForms);
			}
		}
		return true;
	}

	@Override
	public Page list(int pageIndex,int pageSize) {
		List<ReportForms> users = new ArrayList<>();
		Page jsonPage = new Page();
		// 内容
		List<ReportForms> list = reportFormsDao.query();
		// 总数量
		int count = reportFormsDao.count();
		// 最多分页
		int maxTotal = count / pageSize + (count % pageSize > 0 ? 1 : 0);
		// 当前页大于总页
		if (pageIndex > maxTotal) {
			pageIndex = maxTotal;
		} else if (pageIndex < 1) {
			pageIndex =1;
		}
		jsonPage.setPageIndex(pageIndex);
		// 当前开始下标
		int beginIndex = (pageIndex - 1) * pageSize;
		// 结束下标
		int endIndex = beginIndex + pageSize;
		if (endIndex > count) {
			endIndex = count;
		}
		int i = 0;
		for (ReportForms user2 : list) {
			if (i >= beginIndex && i < endIndex) {
				users.add(user2);
			}
			i++;
		}
		jsonPage.setTotal(maxTotal);
		jsonPage.setList(users);
		return jsonPage;
	}

	@Override
	public List<ReportForms> listTime(Date time) throws BizException {
		if (time == null)
			throw new BizException("时间不为null");
		SimpleDateFormat formatters = new SimpleDateFormat("yyyy-MM");
		String formats = formatters.format(time);
		return reportFormsDao.queryTime(formats);
	}

}
